<template>
  <div>
    <div
      class="left"
      style="
        width: calc(100% - 30px);
        height: calc(100vh - 168px);
        border-radius: 4px;
        background: #fff;
        padding: 15px;
      "
    >
    <el-row type="flex" class="row-bg" justify="space-between" style="position: relative;">
        <el-col :span="12"><b>省市区列表</b></el-col>
        <el-col :span="6">
           
        </el-col>
        <span title="收起列表" @click="$emit('show',false)" class="el-icon-d-arrow-left left_show"></span>
    </el-row>
    <div style="height: calc(100vh - 220px);overflow-y: auto;margin-top: 20px;">
        <el-tree
        :props="defaultProps"
        @node-click="handleNodeClick"
        accordion
        node-key="value"
        :default-expanded-keys="activeList"
        :load="loadNode"
        lazy
      ></el-tree>
    </div>
    </div>
  </div>
</template>
<script>
import regions from "ele-admin/packages/regions";
export default {
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "label",
        isLeaf: "leaf",
      },
      arr: ["", "", "", ""],
      arr_name: ["", "", "", ""],

    };
  },
  props:{
    activeList:{
        type:Array,
        default:()=>{
            return []
        }
    }
  },
  methods: {
    handleNodeClick(data) {
      console.log(data);
      if (data.leaf) {
        this.arr[3] = data.value
        this.arr_name[3] = data.label
        //返回结果
        this.$emit("getData", this.arr,this.arr_name);
      }
    },
    loadNode(node, resolve) {
      console.log(node);
      if (node.level === 0) {
        return resolve(regions.cityData);
      }
      if (node.level === 1) {
        this.arr = [node.data.value, "", "", ""];
        this.arr_name[0]=node.data.label
        return resolve(node.data.children);
      }
      if (node.level === 2) {
        console.log(node, node.level);
        this.arr[1] = node.data.value;
        this.arr[2] = "";
        this.arr[3] = "";
        this.arr_name[1]=node.data.label
        return resolve(node.data.children);
      }
      if (node.level === 3) {
        console.log(node.data);
        this.arr[2] = node.data.value;
        this.arr[3] = "";
        this.arr_name[2]=node.data.label
        this.$http
          .get("/user/getStreetsList", {
            params: { district_code: node.data.value },
          })
          .then((res) => {
            resolve(
              res.data.data.map((e) => {
                return {
                  label: e.ext_name,
                  value: e.streets_code,
                  leaf: true,
                };
              })
            );
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      }
    },
  },
};
</script>


<style scoped>
.left_show{
    position: absolute;
    right: -15px;
    top: 0;
    display: inline-block;
    text-align: center;
    line-height: 30px;
    background-color: #1890FF;
    border-top-left-radius: 50%;
    border-bottom-left-radius: 50%;
    width: 30px;
    color: #fff;
    cursor: pointer;
}

</style>